<include file="public@storage"/>
</head>
<link href="__STATIC__/storage/handsontable/handsontable.full.css" rel="stylesheet">
<head>
    <style>
        p {
            margin-top: 30px;
            margin-bottom: 30px;
            font-family: '黑体';
            font-size: 24px
        }
    </style>
</head>
<body>
<div class="container body-content" style="width: 100%;margin-top:2%">
    <div class="panel panel-primary" style="margin-bottom: 0.5rem">
        <div class="panel-heading">
            <button id = "return" style="background-color:rgb(51, 122, 183);border: none ;" onclick="history.go(-1)"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span></button>
            <label class="title">入库单导入</label>
        </div>
        <div class="center-table">
            <button type="button" id="add" class="mui-btn mui-btn--raised mui-btn--primary" style="position: relative;left:82%;top:1%">确认导入</button>
            <div id="hot" style="width:fit-content;width:-moz-fit-content;width:-webket-fit-content;margin:auto;"></div>
        </div>
    </div>
</div>
<script src="__STATIC__/storage/handsontable/handsontable.full.js"></script>
<script>
    $(function () {
        var data = [
            ["采购单号", "供应商号","生成仓库"],
            ["", "", "", ""],
            ["物料号", "条码", "数量"]
        ];
        var hotElement = document.getElementById('hot');
        var hotSettings = {
            data: data,//初始化时候的数据
            minSpareRows:1,//空出多少行
            minRows:30,//最少行
            rowHeights:25,//行的高度
            colWidths:350,//宽度
            rowHeaders:true,
          //  colHeaders:true,
            autoRowSize:true,//
            columnSorting:true//允许排序
        };

        var hot = new Handsontable(hotElement,hotSettings);
        hot.updateSettings({
            cells: function (row, col, prop) {
                var cellProperties = {};

                if (hot.getData()[row][prop] === '采购单号') {
                    cellProperties.readOnly = true;
                }else if(hot.getData()[row][prop] === '生成仓库'){
                    cellProperties.readOnly = true;
                }else if(hot.getData()[row][prop] === '物料号'){
                    cellProperties.readOnly = true;
                }else if(hot.getData()[row][prop] === '条码'){
                    cellProperties.readOnly = true;
                }else if(hot.getData()[row][prop] === '数量'){
                    cellProperties.readOnly = true;
                }else if(hot.getData()[row][prop] === '供应商号'){
                    cellProperties.readOnly = true;
                }
                return cellProperties;
            }
        });
    });

    $("#add").click(function () {
        var sourceData = hot.getData();
        var row = hot.countRows();
        var excelData = new Array(row);
        for (var i = 0; i < row; i++){
            excelData[i] = new Array(3);
        }

        if (sourceData[1][1] == null || sourceData[1][1] === '') {
            layer.msg('请填写供应商号!');
            return;
        }
        if (sourceData[1][2] == null || sourceData[1][2] === '') {
            layer.msg('请填写生成仓库!');
            return;
        }

        excelData[0][0] = sourceData[1][0]; //单号
        excelData[0][1] = sourceData[1][1]; //供应商
        excelData[0][2] = sourceData[1][2]; //仓库

        var k = 1;
        for (var i = 3; i < row; i++) {
            var count = 0;
            if (sourceData[i][0] !== null && sourceData[i][0] !== ''){
                count++;
            }
            if (sourceData[i][1] !== null && sourceData[i][1] !== ''){
                count++;
            }
            if (sourceData[i][2] !== null && sourceData[i][2] !== ''){
                count++;
            }
            if (count === 3) {
                excelData[k][0] = sourceData[i][0];
                excelData[k][1] = sourceData[i][1];
                excelData[k++][2] = sourceData[i][2];
            } else if (count === 1 || count === 2) {
                layer.msg('第'+(i+1)+'行数据不完整');
                return;
            }
        }

        $.ajax({
            url: "{:url('/storage/Purchase/readManual')}",
            dataType: "json",
            type: 'POST',
            data: {
                data: excelData
            },
            success: function (res) {
                if (res.success === true) {
                    layer.msg(res.message);
                    setTimeout(function () {
                        location.reload();
                    },1000);
                } else if (res.success === false) {
                    layer.msg(res.message);
                }
            }, error: function () {
                layer.msg('网络错误');
            }
        })
    })
</script>
</body>
</html>